package org.sgx.gapigui.client.ui.actions.youtube;

import java.util.List;

import org.sgx.gapigui.client.Main;
import org.sgx.gapigui.client.control.ControllerCallback;
import org.sgx.gapigui.client.ui.editableModels.books.Book;
import org.sgx.gapigui.client.ui.editableModels.youtube.Video;
import org.sgx.gwteditors.client.editor.props.PropertyHaver;
import org.sgx.gwteditors.client.impl1.EditorFramework1;
import org.sgx.gwteditors.client.impl1.complex.Array1DEditor;
import org.sgx.gwteditors.client.impl1.complex.Array1DEditor2;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class YoutubeSearch1 extends Composite {

	private static YoutubeSearch1UiBinder uiBinder = GWT.create(YoutubeSearch1UiBinder.class);

	interface YoutubeSearch1UiBinder extends UiBinder<Widget, YoutubeSearch1> {
	}
	
	@UiField VerticalPanel vpanel;
	@UiField TextBox keyword; 
	@UiField Button searchButton; 
	@UiField SimplePanel resultPanel; 
	
	public YoutubeSearch1() {
		initWidget(uiBinder.createAndBindUi(this));
		searchButton.addClickHandler(new ClickHandler() {			
			@Override
			public void onClick(ClickEvent event) {
				doSearch();
			}
		}); 
	}
	protected void doSearch() {
		ControllerCallback<List<Video>> callback = new ControllerCallback<List<Video>>(){

			@Override
			public void call(List<Video> result) {
				doShowResults(result);
			}};
		Main.controller().doYoutubeSearch(keyword.getText(),  callback); 
	}
	protected void doShowResults(List<Video> result) {
		resultPanel.clear(); 
		Array1DEditor<Video> ed = new Array1DEditor<Video>(EditorFramework1.TYPE_PROPERTYHAVER, true);
		ed.load(result.toArray(new Video[]{})); 
		resultPanel.add(ed.getWidget()); 
	}

}
